package game;

import graphic.Assets;
import graphic.animation.Animations;
import graphic.animation.FrameBuilder;
import graphic.drawables.Sprite;

public abstract class AnimatedEntity2D extends Entity2D {
	
	private int frameIndex = 0;
	private Sprite[] sprites;
	private boolean autoUpdate = true;
	private long updateDelay;
	private long lastUpdate;
	
	public AnimatedEntity2D(int entityID, Coordinate coordinate, int shiftAmount) {
		super(entityID, coordinate, shiftAmount);
		sprites = new Sprite[1];
		sprites[0] = Assets.getsLoader().getSprite(Assets.SPRITE_DEFAULT);
	}
	
	public void setAnimation(int IDAnimation){
		sprites =  FrameBuilder.buildFrames(IDAnimation);
	}
	
	public void setAutoUpdate(boolean autoUpdate) {
		this.autoUpdate = autoUpdate;
	}
	
	public  void setUpdateDelay(long updateDelay){
		this.updateDelay = updateDelay;
	}
	
	public void updateFrame(){
		if(frameIndex+1 < sprites.length){
			frameIndex++;
		}else{
			frameIndex = 0;
		}
		lastUpdate = System.currentTimeMillis();
	}
	
	public Sprite getSprite(int index) {
		return sprites[index];
	}
	
	public Sprite getSprite(){
		Sprite sp = sprites[frameIndex];
		if(autoUpdate){
			long now = System.currentTimeMillis();
			if(now-lastUpdate >= updateDelay){
				updateFrame();
			}
		}
		return sp;
	}
	
	@Deprecated
	public Sprite getSprite(boolean autoUpdate ){
		if(autoUpdate){
			updateFrame();
		}
		return sprites[frameIndex];
	}

	
}
